package ${basePackageName}.controller${spotAndSubPack};

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import ${basePackageName}.base.BaseCtl;
import ${basePackageName}.constant.Const;
import ${basePackageName}.service.${subPackAndclassName}Service;
import ${basePackageName}.utils.JsonHelper;
import ${basePackageName}.utils.PageData;
import ${basePackageName}.utils.StringUtils;
import ${basePackageName}.vo.Page;
import ${basePackageName}.vo.PageVo;
import ${basePackageName}.vo.${subPackAndclassName}Vo;

/**
 * <p></p>
 * <p>@Author：${company} - ${author} </p>
 * <p>@Date：${nowDate?string("yyyy年MM月dd日 HH:mm:ss")} </p>
 */
@Controller
@RequestMapping(value = "/${classNameLower}")
public class ${className}Ctl extends BaseCtl {
	@Autowired
	private ${className}Service ${classNameLower}ServiceImpl;

	/**
	 * 请求打开编辑页面
	 * @Auther: ${author} ${nowDate?string("yyyy年MM月dd日 HH:mm:ss")}
	 */
	@RequestMapping(value = { "/edit/{key}" })
	public ModelAndView toEdit(@PathVariable("key") String key) {
		ModelAndView mv = this.getModelAndView();
		try {
			${className}Vo ${classNameLower}Vo = new ${className}Vo();//(${className}Vo.class);
			// 编辑页面
			if (StringUtils.isNotEmpty(key) && !key.equals("0")) {
				${classNameLower}Vo = this.${classNameLower}ServiceImpl.getById(key);
			}
			mv.addObject("pd", JSON.toJSONString(${classNameLower}Vo));
			//mv.addObject("pd", ${classNameLower}Vo);
		} catch (Exception e) {
			logger.error(e);
		}
		return super.toPage(mv, "${subPackageName}/${classNameLower}/edit");
	}

	/**
	 * 保存编辑
	 * @Auther: ${author} ${nowDate?string("yyyy年MM月dd日 HH:mm:ss")}
	 */
	@RequestMapping(value = "/save.do")
	public void save() {
		try {
			${className}Vo ${classNameLower}Vo = super.getPara2Vo(${className}Vo.class);

			String ${pKey[1]} = ${classNameLower}Vo.getId();
			// 编辑
			if (StringUtils.isNotEmpty(${pKey[1]})) {
				${classNameLower}Vo.setModifyUid(getCurrUser().getUserId());
				${classNameLower}ServiceImpl.update(${classNameLower}Vo);
			}
			// 新增
			else {
				${classNameLower}Vo.setId(newId());
				${classNameLower}Vo.setAddUid(super.newId());
				this.${classNameLower}ServiceImpl.add(${classNameLower}Vo);
			}
			outPutJson(getMessage(true));
		} catch (Exception e) {
			logger.error(e);
			outPutJson(getMessage(false, Const.MSG_OP_FAIL));
		}
	}
	
	/**
	 * 查询列表
	 * @Auther: ${author} ${nowDate?string("yyyy年MM月dd日 HH:mm:ss")}
	 */
	@RequestMapping(value = "/query.do")
	public void queryList() {
		try {
			${className}Vo ${classNameLower}Vo = super.getPara2Vo(${className}Vo.class);

			PageVo<${className}Vo> pvPageVo = ${classNameLower}ServiceImpl.query(${classNameLower}Vo, super.getPage());
			String json = JsonHelper.pqPageJson(pvPageVo.getData(), pvPageVo.getPageNo(), pvPageVo.getRecordSize(), pvPageVo.getShowField());

			outPutJson(json);
		} catch (Exception e) {
			logger.error(e);
			outPutJson("");
		}
	}

	/**
	 * 判断是否存在
	 * @Auther: ${author} ${nowDate?string("yyyy年MM月dd日 HH:mm:ss")}
	 */
	 /*
	@RequestMapping(value = "/has.do")
	public void has() {
		try {
			${className}Vo ${classNameLower}Vo = getPara2Vo(${className}Vo.class);

			if (StringUtils.isEmpty(${classNameLower}Vo.get${className}Name())) {
				outPutJson(getMessage(false, Const.MSG_LACK_PARA));
				return;
			}

			if (${classNameLower}ServiceImpl.queryCount(${classNameLower}Vo) > 0) {
				outPutJson(getMessage(true, "数据存在！"));
			} else {
				outPutJson(getMessage(false, "数据不存在！"));
			}
		} catch (Exception e) {
			logger.error(e);
			outPutJson(getMessage(false, Const.MSG_UN_ERROR));
		}
	}
	*/

	/**
	 * 显示列表 
	 * @Auther: ${author} ${nowDate?string("yyyy年MM月dd日 HH:mm:ss")}
	 */
	@RequestMapping(value = "/")
	public ModelAndView index(Page page) {
		return super.toPage("${subPackageName}/${classNameLower}/index");
	}

	/**
	 * 删除数据
	 * @Auther: ${author} ${nowDate?string("yyyy年MM月dd日 HH:mm:ss")}
	 */
	@RequestMapping(value = "/del.do")
	public void del() {
		try {
			PageData pd = getPageData();
			String key = pd.getString("key");

			if (StringUtils.isEmpty(key)) {
				outPutJson(getMessage(false, Const.MSG_LACK_PARA));
				return;
			}
			${classNameLower}ServiceImpl.delete(key);
			outPutJson(getMessage(true));
		} catch (Exception e) {
			logger.error(e);
			outPutJson(getMessage(false));
		}
	}
}